//
//  EditInfoTableViewController.m
//  老司机发车了
//
//  Created by 斌 on 16/8/9.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "EditInfoTableViewController.h"
#import "EditInfoTableViewCell1.h"
#import "EditInfoTableViewCell2.h"

@interface EditInfoTableViewController ()<UITextFieldDelegate>
// 存储label上标题的数组
@property (nonatomic,strong) NSArray *titleArr;
// 存储字典中的键名的数组
@property (nonatomic,strong) NSArray *dicArr;
// 记录用户编辑的数据字典
@property (nonatomic,strong) NSMutableDictionary *dic;
// 获取网上用户的资料数据字典
//@property (nonatomic,strong) NSDictionary *modelDic;

@end

@implementation EditInfoTableViewController


- (NSMutableDictionary *)dic {
    if (!_dic) {
        self.dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}


- (void)viewWillAppear:(BOOL)animated {
    //查找User表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"User"];
    //查找GameScore表里面id为的数据
    [bquery getObjectInBackgroundWithId:[macro shareInstance].userInfo.objectId block:^(BmobObject *object,NSError *error){
        if (error){
            //进行错误处理
        }else{
            //表里有id为xxx的数据
            if (object) {
            self.dic = [object objectForKey:@"userInfo"];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            
            }
        }
    }];
    

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"编辑资料";
    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
    self.navigationItem.leftBarButtonItem = left;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finishAction)];
    self.navigationItem.rightBarButtonItem = right;

    
    [self.tableView registerNib:[UINib nibWithNibName:@"EditInfoTableViewCell1" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EditInfoTableViewCell2" bundle:nil] forCellReuseIdentifier:@"cell2"];

    _dicArr = @[@"nickname",@"gender",@"age",@"birthday",@"star",@"company",@"school",@"position",@"hometown",@"email"];
    _titleArr = @[@"昵称",@"性别",@"年龄",@"生日",@"星座",@"公司",@"学校",@"所在地",@"故乡",@"邮箱"];
}

// 取消方法
- (void)cancelAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 完成方法
- (void)finishAction {
    for (int i = 0; i < 10; i++) {
        UITextField *textfield = [self.view viewWithTag:100 + i];
        [self.dic setValue:textfield.text forKey:_dicArr[i]];
        NSLog(@"%@",textfield.text);
        
    }
    
    UITextView *sign = [self.view viewWithTag:110];
    NSLog(@"------------%@",sign.text);
    [_dic setValue:sign.text forKey:@"sign"];
    //此处是更新操作
    BmobObject *update = [BmobObject objectWithoutDataWithClassName:@"User" objectId:[macro shareInstance].userInfo.objectId];
    [update setObject:_dic forKey:@"userInfo"];
    [update updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"资料修改成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alert addAction:sure];
            [self presentViewController:alert animated:YES completion:nil];
        } else {
            NSLog(@"%@",error);
                }
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        return 5;
    } else if (section == 1) {
        return 5;
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        EditInfoTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.title.text = _titleArr[indexPath.row];
        cell.textfield.tag = indexPath.row + 100;
        cell.textfield.delegate = self;
        cell.textfield.text = [_dic objectForKey:_dicArr[indexPath.row]];

        return cell;
    }else if (indexPath.section == 1) {
        EditInfoTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.title.text = _titleArr[indexPath.row + 5];
        cell.textfield.tag = indexPath.row + 105;
        cell.textfield.delegate = self;
        cell.textfield.text = [_dic objectForKey:_dicArr[indexPath.row + 5]];

        return cell;
    }
    EditInfoTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
    cell.title.text = @"个性签名";
    cell.content.tag = 110;
    cell.content.text = [_dic objectForKey:@"sign"];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return 100;
    }
    return 40;
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
